package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class JDBCDome1 {
    public static void main(String[] args) {
        /*
        * 使用JDBC连接数据库的大致流程：
        * 1.加载驱动
        * 2.使用DriverManager(JDBC提供)再通过加载的驱动与数据库建立连接：Connection对象
        * 3.通过连接对象创建语句对象Statement
        * 4.通过语句对象执行SQL语句
        * 5.如果执行的是查询语句还会得到结果集ResultSet，对其进行遍历获取查询结果
        * 6.关闭连接
        * */
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/mysql?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                    "root",
                    "root"
            );
            System.out.println("连接数据库成功！！!");
            Statement statement = conn.createStatement();

            String sql = "create table users(\n" +
                    "    id int primary key auto_increment,\n" +
                    "    username varchar(50),\n" +
                    "    passdword varchar(50),\n" +
                    "    nickname varchar(50),\n" +
                    "    age int(3)\n" +
                    ")";
            statement.execute(sql);
            System.out.println("创建表成功！！！");
            conn.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
